//
//  PKReadingViewController.m
//  PianKe
//
//  Created by mingtao on 15/10/10.
//  Copyright (c) 2015年 mingtao. All rights reserved.
//

#import "PKReadingViewController.h"
#import "PKRequestManager.h"
#import "PKReadingModel.h"
#import "PKReadingModelList.h"
#import "PKReadingCarousel.h"
#import "PKReadingScouView.h"
#import "PKReadingCollectionViewCell.h"

#import "PKReadingDetailViewController.h"

#import "PKReadingTableViewControllerCell.h"




@interface PKReadingViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>
@property(nonatomic,strong)PKReadingScouView *readingScou;
@property(nonatomic,strong)UITableView *mantableview;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)PKReadingCollectionViewCell *collViewCell;

@property(nonatomic,strong)NSMutableArray *Arraylist;


@property(nonatomic,strong)PKReadingModel *reading;

@end

@implementation PKReadingViewController

-(NSMutableArray *)Arraylist
{
    if (_Arraylist == nil) {
        _Arraylist = [NSMutableArray arrayWithCapacity:0];
    }
    return _Arraylist;
}

-(void)request
{
    NSMutableDictionary *dic =[NSMutableDictionary dictionary];
    
    dic[@"client"] = @"1";
    dic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    dic[@"auth"] = @"BsdsQiiw9K9DCMVzGMTikDJxQ8auML6E82dsXj1lcWP23tBPTxRY4Pw";
    dic[@"version"]= @"3.0.2";
    
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/read/columns" parDic:dic finish:^(NSData *data) {
        NSDictionary *dicData=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        _reading=[[PKReadingModel alloc]init];
        [_reading setValuesForKeysWithDictionary:dicData[@"data"]];
        
        
//        _readingScou = [[PKReadingScouView alloc]initWithFrame:CGRectMake(0, 70, WIHES, HHIHEI/6+40)];
        _readingScou.redingModel = _reading;
        
        NSArray *array=dicData[@"data"][@"list"];
        for (NSDictionary *keyDIct in array) {
            PKReadingModelList *readingmodel=[[PKReadingModelList alloc]init];
            
            [readingmodel setValuesForKeysWithDictionary:keyDIct];
            
            
            [self.Arraylist addObject:readingmodel];
        }
        
        
        
        [self.view addSubview:_readingScou];
     
        [_collectionView reloadData];
        
        
        
    } error:^(NSError *error) {
        NSLog(@"阅读主界面数据请求异常%@",error);
    }];
}





- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.titleLable.text=@"阅读";
    self.view.backgroundColor = [UIColor whiteColor];
    
#pragma mark 执行block方法传值
    __block PKReadingViewController *text=self;
    _readingScou = [[PKReadingScouView alloc]initWithFrame:CGRectMake(0, 70, WIHES, HHIHEI/6+40)];

    _readingScou.imagetagBlock = ^(NSInteger indexpa)
    {
        
        PKReadingDetailViewController *readingDetail=[[PKReadingDetailViewController alloc]init];
        
        

        NSString *strID=[NSString stringWithFormat:@"%@",((PKReadingCarousel *)text.reading.carousel[indexpa]).url];
        NSString *strPP=[strID substringFromIndex:17];
        
       // NSLog(@"%@",strPP);
        
        readingDetail.strtype = strPP;
        
        text.navigationController.navigationBar.hidden = NO;
        [text.navigationController pushViewController:readingDetail animated:YES];
        
    };


    
    
    
    //UICollectionViewFlowLayout
    UICollectionViewFlowLayout *layou =[[UICollectionViewFlowLayout alloc]init];
    
    layou.itemSize = CGSizeMake((WIHES-30)/3, (WIHES-30)/3);
//    //设置相邻距离
    layou.minimumInteritemSpacing = 10;
    
    //两行之间的距离
    layou.minimumLineSpacing = 10;
    layou.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    ////滑动方向 枚举
    layou.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 225, WIHES, WIHES) collectionViewLayout:layou];
//    _collectionView.backgroundColor =[UIColor redColor];
    
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    
    
    [_collectionView registerClass:[PKReadingCollectionViewCell class] forCellWithReuseIdentifier:@"cellView"];
    
    [self request];
    
    [self.view addSubview:_collectionView];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.Arraylist.count;
    
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    PKReadingCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellView" forIndexPath:indexPath];
    
    cell.Readlist = self.Arraylist[indexPath.row];
    
    cell.backgroundColor =[UIColor greenColor];
    
    return cell;
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    PKReadingTableViewControllerCell *tableCell=[[PKReadingTableViewControllerCell alloc]init];
    
    tableCell.listModel = self.Arraylist[indexPath.row];
    
    
    [self.navigationController pushViewController:tableCell animated:YES];

}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/





@end
